<?php
class modelController extends Controller_Base
{
    public function init()
    {
        $this->noLogInJump();
    }
    
    public function indexAction()
    {
        $this->listAction();
    }

    public function listAction()
    {
        $page = intval($this->getParam('page', 1));
        if($page <= 0) $page = 1;
        $style = intval($this->getParam('style', 0));
        if($style <0 || $style >4){
        	$style = 0;
        }
        $search['style'] = $style;
        try {
            if(Model_User::getLevelID() == 1){
            	$models = array();
                $modelTemp = Model_Model::getByName(Model_User::getUserName());
                $cnt = 0;
                if(!empty($modelTemp)){
                	$models[] = $modelTemp;
                	$cnt = 1;
                }
            }else{
                $cnt = Model_Model::getCount($style);
                $models = Model_Model::getList($page, $style);
            }
            $pageNav = Model_Navigate::getPageListBar($cnt, $page, Config_Default::getModelPageSize());
        }catch (Exception $e){
           $this->errExit($e->getTraceAsString(), $e->getMessage());
        }
        $modelStyle = Config_Default::getModelStyle();
        $this->setData('title', '模特列表');
        $this->setData('modellist', true);
        $this->setData('models', $models);
        $this->setData('search', $search);
        $this->setData('modelStyle', $modelStyle);
        $this->setData('pageNav', $pageNav);
        $this->setView('Model/list.tpl');
    }
    
    public function modifyAction()
    {
        if(Model_User::getLevelID() != 2 && Model_User::getLevelID() != 5){
            $this->notAuthWeb();
            return;
        }
        
        $ref = Model_Common::getRef();
        $modelID = $this->getParam('modelid', -1);
        try {
            $modelinfo = Model_Model::getByID($modelID);
            $imgUrl = array();
            if($modelinfo['media'] != ''){
	            $imgArr = explode(",", $modelinfo['media']);
	            foreach($imgArr as $imgName)
	            {
	                $temp = array();
	                $temp['url'] = UPLOAD_PATH . "/{$imgName}";
	                $temp['name'] = $imgName;
	                $imgUrl[] = $temp;
           		}
            }
        }catch (Exception $e){
            $this->errExit($e->getTraceAsString(), $e->getMessage());
        }
        $modelStyle = Config_Default::getModelStyle();
        $modelinfo['media'] = $imgUrl;
        $this->setData('title', '编辑模特');
        $this->setData('model', $modelinfo);
        $this->setData('modelStyle', $modelStyle);
        $this->setData('ref', $ref);
        $this->setView('Model/modify.tpl');
    }
    
    public function detailAction()
    {    
        $modelID = $this->getParam('modelid', -1);
   		$ref = Model_Common::getRef();
        $refArr = explode('/', $ref);
        if(isset($refArr[4]) && $refArr[4] == 'modify'){
        	$ref = 'http://' . HOST . '/model/list';
        }
        try {
            $modelinfo = Model_Model::getByID($modelID);
            
            if(empty($modelinfo)){
                $this->notExistPage('模特不存在', '您访问的模特不存在，即将跳转到首页');
                return ;
            }
            
            if(Model_User::getLevelID() == 1 && trim($modelinfo['modelname']) != trim(Model_User::getUserName())){
                $this->notAuthWeb();
                return;
            }
            $imgUrlTemp = array();
            if($modelinfo['media'] != ''){
	            $imgArr = explode(",", $modelinfo['media']);
	            foreach($imgArr as $imgName)
	            {
	                $imgUrl[] = UPLOAD_PATH . "/{$imgName}";
	            }
	            if(count($imgUrl) == 1)
	            {
	                $imgUrlTemp[] = $imgUrl[0];
	                $imgUrlTemp[] = $imgUrl[0];
	                $imgUrlTemp[] = $imgUrl[0];
	                $imgUrlTemp[] = $imgUrl[0];
	            }
	            else if(count($imgUrl) == 2)
	            {
	                $imgUrlTemp[] = $imgUrl[0];
	                $imgUrlTemp[] = $imgUrl[1];
	                $imgUrlTemp[] = $imgUrl[1];
	                $imgUrlTemp[] = $imgUrl[0];
	            }
                else if(count($imgUrl) == 3)
	            {
	                $imgUrlTemp[] = $imgUrl[0];
	                $imgUrlTemp[] = $imgUrl[1];
	                $imgUrlTemp[] = $imgUrl[2];
	            }else{
	            	$imgUrlTemp = $imgUrl;
	            }
            }
            $works = Model_Model::getMonthWorks($modelinfo['modelname']);
        }catch (Exception $e){
            $this->errExit($e->getTraceAsString(), $e->getMessage());
        }
        $modelinfo['media'] = $imgUrlTemp;
        $this->setData('title', '模特详情');
        $this->setData('model', $modelinfo);
        $this->setData('works', $works);
        $this->setData('ref', $ref);
        $this->setView('Model/detail.tpl');
    }
    
    public function addAction()
    {
        if(Model_User::getLevelID() != 2 && Model_User::getLevelID() != 5){
            $this->notAuthWeb();
            return;
        }
        $modelStyle = Config_Default::getModelStyle();
        
        $this->setData('title', '添加模特');
        $this->setData('modeladd', true);
        $this->setData('modelStyle', $modelStyle);
        $this->setData('levelinfo', Config_Default::getAuthConfig());
        $this->setView('Model/add.tpl');
    }
    
    public function doAddAction()
    {
        if(Model_User::getLevelID() != 2 && Model_User::getLevelID() != 5){
            $this->notAuthAjax();
            return;
        }
        
        $modelname = $this->getPost('modelname', '');
        $nickname = $this->getPost('nickname', '');
        $country = $this->getPost('country', '');
        $height = $this->getPost('height', '');
        $bust = $this->getPost('bust', '');
        $waist = $this->getPost('waist', '');
        $hip = $this->getPost('hip', '');
        $shoescode = $this->getPost('shoescode', '');
        $company = $this->getPost('company', '');
        $beijingstart = $this->getPost('beijingstart', '');
        $beijingend = $this->getPost('beijingend', '');
        $illstart = $this->getPost('illstart', '');
        $illend = $this->getPost('illend', '');
        $illinfo = $this->getPost('illinfo', '');
        $media = $this->getPost('media', '');
		$style = $this->getPost('style', '');

        try {
            $id = Model_Model::add($modelname, $country, $height, $bust, $waist,
                            $hip, $shoescode, $company, $beijingstart, $beijingend, 
                            $illstart, $illend, $illinfo, $media, $style, $nickname);
            $data['id'] = $id;
            $this->ajaxRet(0, 'success', $data);
        }catch (Exception $e){
            $this->ajaxRet($e->getCode(), $e->getMessage());
            return;
        }
    }
    
    public function doModifyAction()
    {
        if(Model_User::getLevelID() != 2 && Model_User::getLevelID() != 5){
            $this->notAuthAjax();
            return;
        }
        
        $modelID = $this->getPost('modelid', '');
        $nickname = $this->getPost('nickname', '');
        $country = $this->getPost('country', '');
        $height = $this->getPost('height', '');
        $bust = $this->getPost('bust', '');
        $waist = $this->getPost('waist', '');
        $hip = $this->getPost('hip', '');
        $shoescode = $this->getPost('shoescode', '');
        $company = $this->getPost('company', '');
        $beijingstart = $this->getPost('beijingstart', '');
        $beijingend = $this->getPost('beijingend', '');
        $illstart = $this->getPost('illstart', '');
        $illend = $this->getPost('illend', '');
        $illinfo = $this->getPost('illinfo', '');
        $media = $this->getPost('media', '');
        $style = $this->getPost('style', '');
        
        try {
            $id = Model_Model::update($modelID, $country, $height, $bust, $waist,
                            $hip, $shoescode, $company, $beijingstart, $beijingend, 
                            $illstart, $illend, $illinfo, $media, $style, $nickname);
            $this->ajaxRet(0, 'success');
        }catch (Exception $e){
            $this->ajaxRet($e->getCode(), $e->getMessage());
            return;
        }
    }
    
    public function doDelAction()
    {
        if(Model_User::getLevelID() != 2 && Model_User::getLevelID() != 5){
            $this->notAuthAjax();
            return;
        }
        
        $modelID = $this->getPost('modelid', '');
        try {
            Model_Model::delete($modelID);
            $this->ajaxRet(0, 'success');
        }catch (Exception $e){
            $this->ajaxRet($e->getCode(), $e->getMessage());
            return;
        }
    }
    
    public function doUploadAction()
    {
        if(Model_User::getLevelID() != 2 && Model_User::getLevelID() != 5){
            $this->notAuthAjax();
            return;
        }

        $uploadFile = 'upload';
        if(empty($_FILES)){
        	$this->ajaxRet(-1, "上传图片过大，请选择不超过3M的图片");
        	return;
        }else if(isset($_FILES[$uploadFile]['error']) && $_FILES[$uploadFile]['error'] != 0){
        	if($_FILES[$uploadFile]['error'] == 1 || $_FILES[$uploadFile]['error'] == 2){
                $error = "上传图片过大，请选择不超过3M的图片";
            }else if($_FILES[$uploadFile]['error'] == 4){
                $error = "请先选择要上传的图片";
            }else {
                $error = "上传图片出错,ErrCode:" . $_FILES[$uploadFile]['error'];
            }
            $this->ajaxRet(-1, $error);
            return;
        }else{
            $tmpName = $_FILES[$uploadFile]['tmp_name'];
            $name = $_FILES[$uploadFile]['name'];
            if(!Model_Validator::picture($name)){
                $this->ajaxRet(-1, "请上传正确格式的图片(jpg,jpeg,gif,png,bmp)");
                return;
            }
            $newName = Model_Common::getUploadFileName($name);
            rename($tmpName, PROJECT_PATH . 'Public/static/upload/' . $newName);
            $data["name"] = $newName;
            $this->ajaxRet(0, "图片{$_FILES[$uploadFile]['name']}上传成功", $data);
        }
    }
}